package com.yswl.yeeduo.ui.task;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.yswl.yeeduo.Constant;
import com.yswl.yeeduo.asynctask.BaseHttpTask;
import com.yswl.yeeduo.ui.bean.ActiviteDetailBean2;
import com.yswl.yeeduo.ui.exception.NetworkException;
import com.yswl.yeeduo.ui.http.HttpUtils;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.ui.tool.ToastUtil;
import com.yswl.yeeduo.ui.tool.UrlParamsConfig;

/**
 * 活动详情
 * 
 * @author yunshuwanli
 * 
 */
public class ActiveDetailInfoTask extends BaseHttpTask<Map<String, String>, Void, Object> {

	public ActiveDetailInfoTask(Context context, int taskId, TaskDelegate delegate) {
		super(context, taskId, delegate);
	}

	/**
	 * 首先执行该方法，在真正的后台操作前北UI线程调用，可在该方法内执行一些准备工作，如：进度条或控件的实例化
	 */
	protected void onPreExecute() {
		super.onPreExecute();
		startProgressDialog();
	}

	@Override
	protected Object doInBackground(Map<String, String>... params) {

		String str = "";
		String url = UrlParamsConfig.getURLEncapsulation(Constant.ConfigNet.get_active_detailinfo, params[0], ServiceAuthManager.getSecret(context));
		try {
			str = HttpUtils.sendGetMethod4Path(url, new HashMap<String, Object>(), "utf-8");
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return new NetworkException();
		} catch (IOException e) {
			e.printStackTrace();
			return new NetworkException();
		}

		return str;
	}

	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		stopProgressDialog();
		if (result instanceof NetworkException) {
			ToastUtil.showShortMsg("网络异常!");
			return;
		}

		JSONObject json = null;
		/**
		 * {"status":0,"msg":"","data":{
		 * "jumpUrl":"http:\/\/wechat.lifedoo.cn\/jump\/s?q=1-1445233397000-IyN5ZWR1byMjNTYyM2MyYThiOTU2N2QyNTRkOGI0OTZmIyN5ZWR1byMjaHR0cCUzQSUyRiUyRm1wLndlaXhpbi5xcS5jb20lMkZzJTNGX19iaXolM0RNekl3TnpBMU5qTTFOQSUzRCUzRCUyNm1pZCUzRDQwMDAzMzg0MiUyNmlkeCUzRDElMjZzbiUzRDcxOTMyZmExODRlM2Y2YzZhMTMxNjA5NDBlZTEyMTcwJTIzcmQ=","title":"\u59d1\u5a18\uff0c\u4f60\u8981\u7684\u662f\u7231\u60c5\u8fd8\u662f\u623f\u5b50","activeId":"5623c2a8b9567d254d8b496f","describe":"\u9762\u5305\u548c\u7231\u60c5\u4f3c\u4e4e\u662f\u5168\u4e16\u754c\u8303\u56f4\u5185\u7684\u604b\u7231\u96be\u9898\u3002\u53ef\u662f\u9762\u5305\u73b0\u5728\u8fdc\u8fdc\u4e0d\u591f\u4e86\uff0c\u5f53\u4e00\u5bf9\u4e00\u5bf9\u60c5\u4fa3\u4e3a\u4e86\u623f\u5b50\u5206\u624b\u7684\u65f6\u5019\uff0c\u7231\u60c5\u597d\u50cf\u53d8\u5f97\u65e0\u8db3\u8f7b\u91cd\u4e86\u3002\u5f88\u591a\u59d1\u5a18\u544a\u8bc9\u6211\u8bf4\uff0c\u6211\u5988\u544a\u8bc9\u6211\uff0c\u8d2b\u7a77\u592b\u59bb\u767e\u4e8b\u54c0\uff0c\u6211\u7238\u5988\u4e0d\u540c\u610f\u4e86\uff0c\u6211\u53ea\u80fd\u5206\u624b\u3002",
		 * "coverImg":"http:\/\/s.lifedoo.cn:8088\/M00\/00\/03\/CqgtmFYjwpeAYRY9AANaXF7Ketg602.png",
		 * "url":"http:\/\/mp.weixin.qq.com\/s?__biz=MzIwNzA1NjM1NA==&mid=400033842&idx=1&sn=71932fa184e3f6c6a13160940ee12170#rd"}}
		 */
		try {
			json = new JSONObject(result + "");
			if (isSuccess(json)) {
				ActiviteDetailBean2 bean = ActiviteDetailBean2.getBean(json.getString(DATA));
				this.getDelegate().success_TaskDelegate(super.getTaskId(), bean);
			}

		} catch (JSONException e) {
			ToastUtil.showShortMsg("数据解析错误");
			return;
		}

	}

	/**
	 * 用户取消线程时调用
	 */
	protected void onCancelled() {
		super.onCancelled();
		stopProgressDialog();
	}

}
